package com.atguigu.syt.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import com.atguigu.common.service.exception.GuiguException;
import com.atguigu.common.util.result.ResultCodeEnum;
import com.atguigu.syt.cmn.listener.RegionExcelListener;
import com.atguigu.syt.cmn.mapper.RegionMapper;
import com.atguigu.syt.cmn.service.RegionService;
import com.atguigu.syt.model.cmn.Region;
import com.atguigu.syt.vo.cmn.RegionExcelVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-05-24
 */
@Service
@Slf4j
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {


    /**
     * @Cacheable：
     * 在方法执行前查看是否有缓存对应的数据，
     * 如果有直接返回数据，如果没有，则调用方法获取数据返回，并缓存起来。
     *
     * value：缓存的名字
     * key：缓存的key
     * unless：条件符合则不缓存，是对出参进行判断，出参用#result表示
     *
     * @param parentCode
     * @return
     */
    //根据上级地区code获取地区列表
    @Override
    @Cacheable(value = "regionList", key = "#parentCode", unless="#result.size() == 0")
    public List<Region> findRegionListByParentCode(String parentCode) {
        LambdaQueryWrapper<Region> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Region::getParentCode,parentCode);
        List<Region> regionList = baseMapper.selectList(queryWrapper);
        regionList.forEach(region -> {
            boolean isHasChildren = region.getLevel().intValue() < 3 ? true : false;
            region.setHasChildren(isHasChildren);
        });
        return regionList;
    }

    //获取地区VO列表
    @Override
    public List<RegionExcelVo> findRegionExcelVoList() {
        List<Region> regionList = this.list();
        ArrayList<RegionExcelVo> regionExcelVoList = ListUtils.newArrayListWithCapacity(regionList.size());
        for (Region region : regionList) {
            RegionExcelVo regionExcelVo = new RegionExcelVo();
            BeanUtils.copyProperties(region,regionExcelVo);
            regionExcelVoList.add(regionExcelVo);
        }
        return regionExcelVoList;
    }

    //地区数据导入Excel
    @Override
    public void importData(MultipartFile file) {
        try {
            long a = System.currentTimeMillis();
            EasyExcel.read(file.getInputStream(), RegionExcelVo.class,
                    new RegionExcelListener(baseMapper)).sheet().doRead();
            long b = System.currentTimeMillis();
            log.info("用时：" +  (a - b) + "ms" );
        } catch (IOException e) {
            throw new GuiguException(ResultCodeEnum.FAIL,e);
        }
    }

    /**
     * 根据地区编码获取地区名称
     * @param code
     * @return
     */
    @Override
    public String getNameByCode(String code) {
        LambdaQueryWrapper<Region> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Region::getCode,code);
        Region region = baseMapper.selectOne(queryWrapper);
        if (region != null){
            return region.getName();
        }
        return " ";
    }
}
